#include "Cannon.h"
#include <math.h>

//Material Properties
GLfloat cannon_diffuse[] = {0.7, 0.7, 0.7, 1.0};
GLfloat cannon_ambient[] = {0.7, 0.7, 0.7, 1.0};
GLfloat cannon_specular[] = {0.5, 0.5, 0.5, 1.0};

// Shininess
GLfloat cannon_shininess = 10.0f;

Cannon::Cannon(void)
{
	width = 0.7;	// Z
	lenght = 1;		// X
	height = 0.6;	// Y
	sizeCannon = 0.25;
	lenghtCannon = 0.7;
	quad = gluNewQuadric();
	loadImage();
}

void Cannon::loadImage()
{
	Image* image1 = loadBMP("textures\\textureBaseCannon.bmp");
	Image* image2 = loadBMP("textures\\textureBaseCannon2.bmp");
	Image* image3 = loadBMP("textures\\textureAlu.bmp");
	Image* image4 = loadBMP("textures\\textureRusty.bmp");
	Image* image5 = loadBMP("textures\\textureCannon.bmp");
	Image* image6 = loadBMP("textures\\textureCannon2.bmp");
	
	textureBase1 = loadTexture(image1);
	textureBase2 = loadTexture(image2);
	textureBaseCannon1 = loadTexture(image3);
	textureBaseCannon2 = loadTexture(image4);
	textureCannon1 = loadTexture(image5);
	textureCannon2 = loadTexture(image6);

	currentTextureBase = textureBase1;
	currentTextureBaseCannon = textureBaseCannon1;
	currentTextureCannon = textureCannon1;
	
	delete image1;
	delete image2;
	delete image3;
	delete image4;
	delete image5;
	delete image6;
}

void Cannon::switchTexture()
{
	if(currentTextureBase == textureBase1)
		currentTextureBase = textureBase2;
	else
		currentTextureBase = textureBase1;
	if(currentTextureBaseCannon == textureBaseCannon1)
		currentTextureBaseCannon = textureBaseCannon2;
	else
		currentTextureBaseCannon = textureBaseCannon1;
	if(currentTextureCannon == textureCannon1)
		currentTextureCannon = textureCannon2;
	else
		currentTextureCannon = textureCannon1;
}

void Cannon::display()
{
	glPushMatrix();
		glTranslatef(0.0, elevation, 0.0);

		glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, cannon_diffuse);
		glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, cannon_ambient );
		glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, cannon_specular);
		glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, cannon_shininess);

		glTranslatef(0.0, height/2, -(1-width)/2);

		glBindTexture(GL_TEXTURE_2D, currentTextureBase);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		// CUBE
		glPushMatrix();
			glColor3f(1.0f, 1.0f, 1.0f);
			glScalef(lenght, height, width);
			drawCube(0.35, 0.65, 0.5, 0.8);
		glPopMatrix();
			/*	
		// TRIANGLE 1
		glPushMatrix();
			glTranslatef(-lenght/2, -height/2, width/2);
			glBegin(GL_TRIANGLES);
				glColor3f(0.4,0.4,0.4);
				glVertex3f(0.0, 0.0, 0.0);
				glVertex3f(0.0, 0.0, 1-width);
				glVertex3f(0.0, height, 0.0);
			glEnd();
		glPopMatrix();
		// TRIANGLE 2
		glPushMatrix();
			glTranslatef(lenght/2, -height/2, width/2);
			glBegin(GL_TRIANGLES);
				glColor3f(0.4,0.4,0.4);
				glVertex3f(0.0, 0.0, 0.0);
				glVertex3f(0.0, 0.0, width/2);
				glVertex3f(0.0, height, 0.0);
			glEnd();
		glPopMatrix();
		// TRIANGLE 3
		glPushMatrix();
			glTranslatef(0.0, -height/2, width/2);
			glBegin(GL_TRIANGLES);
				glColor3f(0.4,0.4,0.4);
				glVertex3f(0.0, 0.0, 0.0);
				glVertex3f(0.0, 0.0, width/2);
				glVertex3f(0.0, height, 0.0);
			glEnd();
		glPopMatrix();*/
		glPushMatrix();
			glDisable(GL_CULL_FACE);
			glColor3f(1.0f, 1.0f, 1.0f);
			// RECTANGLE 1
			glPushMatrix();
				glTranslatef(-lenght/2, -height/2, width/2);
				glBegin(GL_QUADS);
					glNormal3f(-1,0,0);
					glTexCoord2f(0.8f, 0.0f); glVertex3f(0.0, 0.0, 0.0);
					glTexCoord2f(0.95f, 0.0f); glVertex3f(0.0, 0.0, 1-width);
					glTexCoord2f(0.95f, 0.3f); glVertex3f(0.0, height, 1-width);
					glTexCoord2f(0.8f, 0.3f); glVertex3f(0.0, height, 0.0);
				glEnd();
			glPopMatrix();
			// RECTANGLE 2
			glPushMatrix();
				glTranslatef(lenght/2, -height/2, width/2);
				glBegin(GL_QUADS);
					glNormal3f(1,0,0);
					glTexCoord2f(0.8f, 0.0f); glVertex3f(0.0, 0.0, 0.0);
					glTexCoord2f(0.95f, 0.0f); glVertex3f(0.0, 0.0, 1-width);
					glTexCoord2f(0.95f, 0.3f); glVertex3f(0.0, height, 1-width);
					glTexCoord2f(0.8f, 0.3f); glVertex3f(0.0, height, 0.0);
				glEnd();
			glPopMatrix();
			// RECTANGLE UP
			glPushMatrix();
				glTranslatef(-lenght/2, height/2, width/2);
				glBegin(GL_QUADS);
					glNormal3f(0,1,0);
					glTexCoord2f(0.0f, 0.65f); glVertex3f(0.0, 0.0, 0.0);
					glTexCoord2f(0.0f, 0.8f); glVertex3f(0.0, 0.0, 1-width);
					glTexCoord2f(0.5f, 0.8f); glVertex3f(lenght, 0.0, 1-width);
					glTexCoord2f(0.5f, 0.65f); glVertex3f(lenght, 0.0, 0.0);
				glEnd();
			glPopMatrix();
			glEnable(GL_CULL_FACE);
		glPopMatrix();

		// CANNON
		for(int i=0; i!=2; ++i){
			glPushMatrix();
				glTranslatef(pow(-1.0, i)*lenght/4, 0.0, (width/2 + lenghtCannon/6));
				// BASE
				glPushMatrix();
					glBindTexture(GL_TEXTURE_2D, currentTextureBaseCannon);
					glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
					glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
					glColor3f(1.0f, 1.0f, 1.0f);

					glScalef(sizeCannon, sizeCannon, lenghtCannon/3);
					drawCube();
				glPopMatrix();
				
				// POINTE
				glPushMatrix();
					glBindTexture(GL_TEXTURE_2D, currentTextureCannon);
					glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
					glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
					glColor3f(1.0f, 1.0f, 1.0f);

					gluQuadricNormals(quad, GLU_SMOOTH);   // Create Smooth Normals (better for light)
					gluQuadricTexture(quad, GL_TRUE);      // Create Texture Coords
					glPushMatrix();
						gluCylinder (quad, sizeCannon/4, sizeCannon/4, lenghtCannon, 10, 30);
					glPopMatrix();
					glPushMatrix();
						glTranslatef(0.0,0.0,lenghtCannon);
						glColor3f(0,0,0);
						glNormal3f(0,0,1);
						gluDisk(quad, 0.0, sizeCannon/4, 10,32);
					glPopMatrix();
				glPopMatrix();
			glPopMatrix();
		}
	glPopMatrix();
}

float Cannon::getHeight(){
	return height;
}

Cannon::~Cannon(void)
{
	gluDeleteQuadric(quad);
}
